/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package hextd.message;

import hextd.message.MessageFactory.NetworkMessage;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageFetch implements Message {

	public enum FetchType {

		mp,
		sp,
		maplist,
		map
	}
	public NetworkMessage type = NetworkMessage.Fetch;
	public static String RegExp = "FETCH ([a-z]+) ([-0-9]+)";
	public static Pattern MessagePattern = Pattern.compile(MessageFetch.RegExp);
	//
	private String message;
	private int fetchId;
	private FetchType fetchType;

	public static String formatMessage(int fetchId, FetchType fetchType) {
		String message;
		message = "FETCH " + fetchType.name() + " " + fetchId;
		return message;
	}

	public MessageFetch() {
	}

	private MessageFetch(String message) {
		this.message = message;
		Matcher matcher = MessageFetch.MessagePattern.matcher(message);
		if (matcher.matches()) {
			try {
				this.fetchType = FetchType.valueOf(matcher.group(1));
				this.fetchId = Integer.parseInt(matcher.group(2));
			} catch (NumberFormatException e) {
				System.err.println("MessageSay: invalid id in: " + matcher.group(2));
			}
		}
	}

	@Override
	public String getMessage() {
		return this.message;
	}

	@Override
	public Message initFromString(String message) {
		return new MessageFetch(message);
	}

	public int getFetchId() {
		return fetchId;
	}

	public FetchType getFetchType() {
		return fetchType;
	}

	@Override
	public Pattern getPattern() {
		return MessageFetch.MessagePattern;
	}

	@Override
	public NetworkMessage getType() {
		return this.type;
	}

	@Override
	public int getSenderId() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void setSenderId(int id) {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
